package com.meisiabc.support.wechat;

import com.alibaba.fastjson.JSON;
import com.riversoft.weixin.app.AppWxClientFactory;
import com.riversoft.weixin.app.base.AppSetting;
import com.riversoft.weixin.common.WxClient;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;

/**
 * 二维码工具
 */

@Slf4j
public class WechatQrCodes {
    private WxClient wxClient;

    public static WechatQrCodes defaultQrCodes() {
        return with(AppSetting.defaultSettings());
    }

    public static WechatQrCodes with(AppSetting appSetting) {
        WechatQrCodes qrCodes = new WechatQrCodes();
        qrCodes.setWxClient(AppWxClientFactory.getInstance().with(appSetting));
        return qrCodes;
    }

    public void setWxClient(WxClient wxClient) {
        this.wxClient = wxClient;
    }

    public InputStream create(String path, String scene) {
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit";
        String post = JSON.toJSONString(QrCodesConfig.builder().scene(scene).page(path).build());
        log.info("getwxacodeunlimit. [post={}]", post);
        return wxClient.copyStream(url, post);
    }
}
